//
//  XKNewsViewController.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/10.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKNewsViewController.h"
#import "XKNewsHeaderSelectView.h"
#import "XKNewsCell.h"
#import "XKNewsHtmlController.h"
#import "XKNewsModel.h"

static NSString *XKNewsCellID = @"XKNewsCellReuseIdentifier";

@interface XKNewsViewController () <UITableViewDataSource, UITableViewDelegate, XKNewsSelectViewDelegate>

@property (nonatomic, strong) XKNewsHeaderSelectView *selectView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, assign) NSInteger dataType;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *navigationView;

@end

@implementation XKNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self initSubViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}



#pragma mark - 设置UI相关
- (void)setupUI {
    self.navigationItem.title = @"资讯";
    _dataType = 2;
}


#pragma mark - 添加子视图
- (void)initSubViews {
    [self.view addSubview:self.navigationView];
    [self.navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];

    [self.view addSubview:self.selectView];
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navigationView.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(45*Scale_H);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(Main_Width);
        make.top.mas_equalTo(self.selectView.mas_bottom);
        make.bottom.mas_equalTo(self.view).offset(-49);
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDataWithType:_dataType];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark - 懒加载tableView
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsHorizontalScrollIndicator   = NO;
        _tableView.separatorStyle                   = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor                  = kBaseGrayColor;
        _tableView.tableFooterView                  = [UIView new];
        _tableView.estimatedRowHeight               = 300;
        _tableView.rowHeight                        = UITableViewAutomaticDimension;
        _tableView.dataSource                       = self;
        _tableView.delegate                         = self;
        
        [_tableView registerClass:[XKNewsCell class] forCellReuseIdentifier:XKNewsCellID];
    }
    return _tableView;
}

#pragma mark - 懒加载顶部按钮试图
- (XKNewsHeaderSelectView *)selectView {
    if (!_selectView) {
        _selectView = [XKNewsHeaderSelectView new];
        _selectView.delegate = self;
        [_selectView.leftBtn setTitle:@"最新动态" forState:UIControlStateNormal];
        [_selectView.rightBtn setTitle:@"官方公告" forState:UIControlStateNormal];
    }
    return _selectView;
}




#pragma mark - NavigationView
- (UIView *)navigationView {
    UIView *topView = [UIView new];
    topView.backgroundColor = kBaseRedColor;
    topView.frame = CGRectMake(0, 0, Main_Width, 64);
    [self.view addSubview:topView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"资讯";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:17];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView);
        make.centerY.mas_equalTo(topView).offset(10);
    }];
    return topView;
}




#pragma mark - 顶部按钮代理
- (void)btnClickWithBtn:(UIButton *)clickBtn {
    _dataType = clickBtn.tag == 1 ? 2 : 1;
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - tableView数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XKNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:XKNewsCellID];
    if (!cell) {
        cell = [[XKNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XKNewsCellID];
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

#pragma mark - tableView代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self pushWebViewWithDataModel:self.dataArray[indexPath.row]];
}

- (void)pushWebViewWithDataModel:(Data *)dataModel {
    XKNewsHtmlController *htmlVC = [XKNewsHtmlController new];
    htmlVC.title = dataModel.title;
    htmlVC.htmlString = dataModel.content;
    htmlVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:htmlVC animated:YES];
}

#pragma mark - 加载网络数据
- (void)loadDataWithType:(NSInteger)type {
    
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_News parameters:@{@"typeid":@(type)} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        [self.tableView.mj_header endRefreshing];
        XKNewsModel *model = [XKNewsModel mj_objectWithKeyValues:successDict];
        if ([model.flag isEqualToString:@"Success"]) {
            self.dataArray = model.data;
            [self.tableView reloadData];
        }
    } failureBlock:^(NSDictionary *failureDict) {
        [self.tableView.mj_header endRefreshing];
    } errorBlock:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
